package cn.zjj.controller;

import cn.zjj.domain.Product;
import cn.zjj.downgrade.DowngradeUtils;
import cn.zjj.service.IProductService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PostMapping("/add")
    @SentinelResource(value = "product-add",blockHandler = "blockMethod",blockHandlerClass = DowngradeUtils.class)
    public Map<String,Object> add(@RequestBody Product product){

        try {
            return productService.insert(product);
        } catch (Exception e) {
            e.printStackTrace();
            HashMap<String,Object> exceptionMap=new HashMap<>();
            exceptionMap.put("success",false);
            exceptionMap.put("message","服务器异常");
            return exceptionMap;
        }

    }
}
